<?php
/**
 * Created by GZX
 * Datetime: 2025-05-08
 */

class Screwim
{

    protected static $bin = 'screwim83';
    /**
     * 创建目录
     * @param $dir  目录名
     * @return boolean true 成功， false 失败
     */
    static public function mk_dir($dir) {
        $dir = rtrim($dir,'/').'/';
        if(!is_dir($dir))
        {
            if(mkdir($dir, 0700, true)==false)
            {
                return false;
            }
            return true;
        }
        return true;
    }

    /**
     * 删除文件夹下没有子文件夹
     * @param $dir      原目录
     * @return boolean true 成功, false 失败
     */
    public static  function deldir($dir) {
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . "/" . $file;
                if (!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    self::deldir($fullpath);
                }
            }
        }
        closedir($dh);
        if (rmdir($dir)) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 删除整个文件夹包括所有的子文件夹
     * @param $dir      原目录
     * @return boolean true 成功, false 失败
     */
    public static function deleDirAllSpe($dir) {
        $dh = opendir($dir);
        $dir0 = $dir . "/0";
        if (is_dir($dir0)) {
            self::deleDirAllSpe($dir0);
        }
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . "/" . $file;
                if (!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    self::deleDirAllSpe($fullpath);
                }
            }
        }
        closedir($dh);
        if (rmdir($dir)) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 复制目录
     * @param $surDir      原目录
     * @param $toDir      目标目录
     * @return boolean true 成功, false 失败
     */
    static function copy_dir($surDir,$toDir) {
        $surDir = rtrim($surDir,'/').'/';
        $toDir = rtrim($toDir,'/').'/';
        if (!file_exists($surDir))
        {
            return  false;
        }

        if (!file_exists($toDir))
        {
            self::mk_dir($toDir);
        }
        $file = opendir($surDir);
        while ($fileName = readdir($file))
        {
            $file1 = $surDir .'/'.$fileName;
            $file2 = $toDir .'/'.$fileName;
            if ($fileName != '.' && $fileName != '..')
            {
                if (is_dir($file1))
                {
                    self::copy_dir($file1, $file2);
                }
                else
                {
                    copy($file1, $file2);
                }
            }
        }
        closedir($file);
        return true;
    }

    /**
     * 列出目录
     * @param $dir  目录名
     * @return 目录数组。列出文件夹下内容，返回数组 $dirArray['dir']:存文件夹；$dirArray['file']：存文件
     */
    static function get_dirs($dir) {
        $dir = rtrim($dir,'/').'/';
        $dirArray = [];
        if (false != ($handle = opendir ( $dir )))
        {
            $i = 0;
            $j = 0;
            while ( false !== ($file = readdir ( $handle )) )
            {
                if (is_dir ( $dir . $file ))
                { //判断是否文件夹
                    $dirArray ['dir'] [$i] = $file;
                    $i ++;
                }
                else
                {
                    $dirArray ['file'] [$j] = $file;
                    $j ++;
                }
            }
            closedir ($handle);
        }
        return $dirArray;
    }

    /**
     *加密PHP文件
     * @
     */

    public static function encode($path){
        $copyPath = rtrim($path,"/")."_enc";
        self::copy_dir($path,$copyPath);

        self::make($copyPath,"");
        return $copyPath;
    }


    public static function make($path,$screwType = ""){
        if(is_dir($path)){
            $path = rtrim($path,"/")."/";
            $oriFile = self::get_dirs($path);
            if(isset($oriFile['file'])){

                foreach ($oriFile['file'] as $file){
                    if(substr($file,-3) === 'php'){
                        shell_exec(self::$bin." ".$path.$file.$screwType);
                        if(file_exists($path.$file.".screw")) {
                            shell_exec("mv -f " . $path . $file . ".screw " . $path . $file);
//                            unlink($path.$file.".screw");
                        }elseif (file_exists($path.$file.".discrew")){
                            shell_exec("mv -f " . $path . $file . ".discrew " . $path . $file);
                        }
                    }
                }

            }

            foreach ($oriFile['dir'] as $dir) {
                if($dir == "." || $dir == "..") continue;
                self::make($path.$dir,$screwType);
            }

        }
    }

}

//需要加密代码的文件目录
$path_dir = [
    './app',
    './config',
    './extend'
    
];

foreach ($path_dir as $path_file) {
    $screwname = Screwim::encode($path_file);
    shell_exec("rm -rf {$path_file} && mv {$path_file}_enc {$path_file} && chown apache: -R {$path_file}");
}